#ifndef POLYNOMIAL_HPP
#define POLYNOMIAL_HPP


#include <map>
#include "functor.hpp"

namespace TermStructure {
    //! A Polynomial
    /*! A Polynomial of 
     */
    template <typename T,typename TOrder = size_t>
    class Polynomial : AbstractFunctor<T>
    {
    private:
        std::map<TOrder, T> _structure;
        void _addToOrder(const TOrder &order)
        {
            for(TOrder i = _structure.size(); i <= order; i++)
            {
                _structure.insert(std::pair<TOrder,T>(i, 0));
            }
        }
    public:
        Polynomial(const TOrder& order = 0)
        {
            _addToOrder(order);
        }
        
        virtual T operator()(const T& x) const
        {
            T result = 0;
            T r = 1;
            for(typename std::map<TOrder,T>::const_iterator i = _structure.begin(); i !=_structure.end(); i++)
            {
                result += i->second * r;
                r *= x;
            }
            return result;
        }
        
        T &operator[](const TOrder &i)
        {
            if(i<=_structure.size())
            {
                _addToOrder(i);
            }
            return _structure[i];
        }
        const T&operator[](const TOrder &i) const
        {
            return _structure.find(i)->second;
        }
    };
    template <typename T, typename TOrder> 
    Polynomial<T,TOrder> GenerateBasis(TOrder order)
    {
        
    }
}


#endif
